//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================



using System;
using System.Collections.Generic;
using System.Text;
using System.Transactions;
using System.Globalization;
using System.Threading;

using Shp.Runtime.Contract;

namespace Shp.Runtime.Services
{
    public class PresentationService : IPresentationUC, IPresentationSC
    {
        public int GetConfiguredLayout(string tenantAlias)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InstalledUICulture;

            if (string.IsNullOrEmpty(tenantAlias))
                throw new ArgumentNullException("tenantAlias");

            return PresentationLogic.GetConfiguredLayout(tenantAlias);
        }

        public string GetConfiguredStyle(string tenantAlias)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InstalledUICulture;

            if (string.IsNullOrEmpty(tenantAlias))
                throw new ArgumentNullException("tenantAlias");

            return PresentationLogic.GetConfiguredStyle(tenantAlias);
        }

        public StringPair[] GetMainMenu(string tenantAlias, bool isPublic)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InstalledUICulture;

            if (string.IsNullOrEmpty(tenantAlias))
                throw new ArgumentNullException("tenantAlias");

            return PresentationLogic.GetMainMenu(tenantAlias, isPublic);
        }

        public KeyValuePair<int, string>[] GetLayoutList()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InstalledUICulture;

            Guid tenantId = Context.TenantId;
            if (tenantId == Guid.Empty)
                throw new System.Security.SecurityException();

            return PresentationLogic.GetLayoutList(tenantId);
        }

        public KeyValuePair<Guid, string>[] GetStyleList()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InstalledUICulture;

            Guid tenantId = Context.TenantId;
            if (tenantId == Guid.Empty)
                throw new System.Security.SecurityException();

            return PresentationLogic.GetStyleList(tenantId);
        }

        public void UpdatePresentation(int layout, Guid style)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InstalledUICulture;

            Guid tenantId = Context.TenantId;
            if (tenantId == Guid.Empty)
                throw new System.Security.SecurityException();
            if (layout < 0)
                throw new ArgumentOutOfRangeException("layout");
            if (style == Guid.Empty)
                throw new ArgumentNullException("style");

            PresentationLogic.UpdatePresentation(tenantId, layout, style);
        }

        public void AddStyle(string name, Uri url)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InstalledUICulture;

            Guid tenantId = Context.TenantId;
            if (tenantId == Guid.Empty)
                throw new System.Security.SecurityException();
            if (string.IsNullOrEmpty(name))
                throw new ArgumentNullException("name");

            PresentationLogic.AddStyle(tenantId, name, url);
        }

        public MainMenuItem[] GetMainMenuList()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InstalledUICulture;

            Guid tenantId = Context.TenantId;
            if (tenantId == Guid.Empty)
                throw new System.Security.SecurityException();

            return PresentationLogic.GetMainMenuList(tenantId);
        }

        public void UpdateMainMenu(MainMenuItem[] menuItems)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InstalledUICulture;

            Guid tenantId = Context.TenantId;
            if (tenantId == Guid.Empty)
                throw new System.Security.SecurityException();

            using (TransactionScope scope = new TransactionScope())
            {
                try
                {
                    PresentationLogic.UpdateMainMenu(tenantId, menuItems);
                }

                catch
                {
                    throw;
                }

                scope.Complete();
            }
        }

        public Guid GetCurrentStyleId()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InstalledUICulture;

            Guid tenantId = Context.TenantId;
            if (tenantId == Guid.Empty)
                throw new System.Security.SecurityException();

            return PresentationLogic.GetCurrentStyleId(tenantId);
        }
    }
}
